package vm.baseapi;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LocalProcessSocket {
    private Process process;
    private InputStream processInput;
    private OutputStream processOutput;

    public LocalProcessSocket() {

    }

    public void run(String exec) throws Exception {
        process = Runtime.getRuntime().exec(exec);

        this.processInput = process.getInputStream();
        this.processOutput = process.getOutputStream();
    }

    // 获取本地程序的输出流
    public OutputStream getOutput() {
        return processOutput;
    }

    // 获取本地程序的输入流
    public InputStream getInput() {
        return processInput;
    }

    // 关闭资源
    public void close() {
        if (process != null) {
            process.destroy();
        }
        if (processInput != null) {
            try {
                processInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (processOutput != null) {
            try {
                processOutput.close();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }
}
